#pragma once

#include "MemoryPool.h"

using namespace std;

class Ojb
{
public:
	Ojb():i(1), j(2)
	{
	}
	~Ojb()
	{
		cout<<"~P is called\n";
	}

	ostream& operator<<(ostream& stream)
	{
		stream<<i<<'/'<<j;
		return stream;
	}

	void* operator new(size_t size)
	{
		return m_pool.malloc();
	}

	void operator delete(void* addr)
	{
		m_pool.free(addr);
	}

	friend ostream& operator<<(ostream& stream, const Ojb& obj);

private:
	int i,j;
	static CMemPool m_pool;
};

inline ostream& operator<<(ostream& stream, const Ojb& obj)
{
	stream<<obj.i<<'/'<<obj.j;
	return stream;
}

class Str
{
public:
	explicit Str(const char* str)
	{
		if(str)
		{
			m_data = new char[strlen(str) + 1];
			strcpy(m_data, str);
		}
		else
		{
			m_data = new char[1];
			m_data[0] = 0;
		}
	}
	Str(const Str& ref)
	{
		(*this) = ref;
	}
	Str& operator=(const Str& ref)
	{
		m_data = new char[strlen(ref.m_data) + 1];
		strcpy(m_data, ref.m_data);
		return *this;
	}
	char& operator[](size_t index)
	{
		if(index >= strlen(m_data))
			index = 0;
		return m_data[index];
	}
	const char& operator[](size_t index) const
	{
		if(index >= strlen(m_data))
			index = 0;
		return m_data[index];
	}
	friend ostream& operator<<(ostream& stream, const Str& str);

private:
	
	char* m_data;
};

inline ostream& operator<<(ostream& stream, const Str& str)
{
	cout<<str.m_data<<endl;
	return stream;
}
